//index.js
//获取应用实例
const app = getApp()
const Api = require("../../utils/api.js").API
const Store = require("../../utils/store.js").store

const codeZ = require("../../utils/util.js")
Page({
    data: {
       
        tabsArray: [],
        list: [],
        codeList: [],
        active: 0,
        spuNo: " ",
        page: 1,
        colName: '',
        total: 0,
        isShow: false,
        goodsName: '',
        heizhi:100,
        hei2:300,
         showCarid:false,
        actions: [
             {name: '购买商品'},
            {
              name: '购买礼品卡',
             },
             
          ],
          itemZhi:{},
    },
    onLoad() {

    },
    onShow: function() {
       this.setData({
            heizhi:(app.globalData.screenWidth-20)/2,
            hei2:(app.globalData.screenHeight-120-100),
       })
        let that = this;
        Api.getLookGoodsSpuApi().then(res => {
            if (res.code == 0) {
                var resData = res.data
                if (resData) {
                    let data;
                    if (this.data.spuNo != "") {
                        resData.unshift({
                            "spuNo": " ",
                            "spuName": "全部商品",
                            "parentId": "0",
                            "subSpus": []
                        })
                        that.setData({
                            tabsArray: resData,
                            active: this.data.spuNo,
                            page:1
                        })
                        data = {
                            "asc": true,
                            "limit": 10,
                            "page": 1,
                            "sidx": "",
                            "spuNo": this.data.spuNo
                        }
                    } else {
                        resData.unshift({
                            "spuNo": " ",
                            "spuName": "全部商品",
                            "parentId": "0",
                            "subSpus": []
                        })
                        that.setData({
                            tabsArray: resData,
                            active: resData[0].spuNo,
                            page:1
                        })
                        data = {
                            "asc": true,
                            "limit": 10,
                            "page": 1,
                            "sidx": "",
                            "spuNo": resData[0].spuNo,
                        }
                    }
                    that.getShopList(data, '1');
                }
            }
        })
    },
    onChange(event) {
        let that = this;
        let dict = event.detail;
        let data = {
            "asc": true,
            "limit": 10,
            "page": 1,
            "sidx": "",
            "spuNo": dict.name,
        }
        this.setData({
            spuNo: dict.name,
            page: 1,
            total: 0,
            list: [],
            isShow: false

        })
        this.getShopList(data, '2');
    },
    getShopList: function(data, type) {
        let that = this;
        Api.lookGoodsPageApi(data).then(res => {    
            if (res.code == 0) {
                let data = res.data.records;
                if (data.length == 0) {
                    return;
                }
                data.forEach(ite => {
                    if (ite.checkedNum == null) {
                        ite.checkedNum = 0;
                    }
                })
                var pageNo = this.data.page - 1; 
                that.setData({

                    ['list[' + pageNo + ']']: data,
                    total: res.data.total
                })
                console.log(this.data.list);
             }
        })
    },
    refreshBottom: function() {
        var that = this
        var a = this.data.page;
        var listArr = this.data.list.length;
        if (listArr == this.data.total) {
            this.setData({
                isShow: true
            })
            return
        }
        a++;
        that.setData({
            page: a
        })
        let data = {
            "asc": true,
            "limit": 10,
            "page": a,
            "sidx": "",
            "spuNo": this.data.spuNo,
        }

        this.getShopList(data, '2');


    },

    onSerach(e) {
        console.log(e);

        this.setData({
            goodsName: e.detail
        })
    },
    toGoodLookAdd() {
        wx.navigateTo({
          url: '../GoodsLookAdd/index',
        })
    },
    onSureSearch() {

        this.setData({
            list: [],
            active: this.data.spuNo,
            isShow: false

        })

        let data = {
            "asc": true,
            "limit": 10,
            "page": 1,
            "sidx": "",
            "spuNo": this.data.spuNo,
            goodsName: this.data.goodsName
        }

        this.getShopList(data, '1');

    },
    goodsDetail:function(e){
        var item = e.currentTarget.dataset.item;
        console.log(item);
        wx.navigateTo({
          url: '../GoodsLookDetail/index',
          events: { },
          success: function(res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('acceptDataFromOpenerPage', { item: item })
          }
        })
    },
   
     onClose() {
        this.setData({ showCarid: false });
      },
    
   

});
